// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   Drawer.java

package canvas;

import javax.microedition.lcdui.*;

// Referenced classes of package mquran:
//            QuranCanvas, SUtils

public class Drawer
{

    public Drawer(QuranCanvas qCanvas)
    {
        canvas = qCanvas;
    }

    public final void draw(Graphics g, int pilih)
    {
        String playayats = "";
        int top = QuranCanvas.font.getHeight();
        if(QuranCanvas.cur_surah != null && QuranCanvas.cur_surah.length() != 0)
            g.setFont(QuranCanvas.font);
        SUtils.drawColorDeg(g, 0xcac552, 0x775c3b, 0, 0, canvas.getWidth(), top + 1, true);
        g.setColor(0);
        QuranCanvas.cur_surah = replaceAll(QuranCanvas.cur_surah, "()", "");
        g.drawString(QuranCanvas.cur_surah, 0, 0, 20);
        g.drawLine(0, QuranCanvas.font.getHeight() + 1, g.getClipWidth(), QuranCanvas.font.getHeight() + 1);
//        if(QuranCanvas.cur_page == 1 && canvas.selected_surah != 1 && canvas.selected_surah != 9)
//        {
////            g.drawImage(canvas.img_basmalah, g.getClipWidth() / 2, top + 2, 17);
////            top += canvas.img_basmalah.getHeight();
//        }
        int i = QuranCanvas.page_line_start[QuranCanvas.cur_page];
        if(QuranCanvas.cur_page == 1)
            i = 0;
        if(i >= QuranCanvas.page)
            return;
        boolean aflag[] = new boolean[1];
        short end_line = QuranCanvas.page_line_start[QuranCanvas.cur_page + 1];
        char ac[] = new char[300];
        boolean flag7 = false;
        g.setColor(0);
        Quran.StopSound();
        for(; i < end_line; i++)
        {
            aflag[0] = false;
            long l2 = QuranCanvas.buffer_int_au[i + 1];
            int j1 = (int)(l2 >> 10) - 1;
            int k1 = (int)(l2 & 1023L) + j1 + 1;
            if(QuranCanvas.buffer_short_M[i + 1] != canvas.cur_index)
                canvas.loadBin(QuranCanvas.buffer_short_M[i + 1]);
            if(j1 < 0)
                continue;
            try
            {
                int i2 = k1 - j1;
                int j2 = 0;
                int i1 = 0;
                for(int l = j1; l++ < k1; i1++)
                {
                    if(l > QuranCanvas.buffer_len - 1)
                    {
                        i2 = j1 - l;
                        flag7 = true;
                        break;
                    }
                    if(QuranCanvas.lines_type[i + 1] == '\001')
                    {
                        byte byte0;
                        if((byte0 = (byte)canvas.a(l, aflag)) != 0 && QuranCanvas.getByte(QuranCanvas.buffer[l]) != 0 && QuranCanvas.getByte(QuranCanvas.buffer[l]) != 35)
                            canvas.buffer_line_arabic[j2++] = byte0;
                        continue;
                    }
                    if(QuranCanvas.buffer[l] != 10 && QuranCanvas.buffer[l] != 0)
                        ac[i1] = (char)QuranCanvas.buffer[l];
                }

                short line = QuranCanvas.ayat_line[i + 1];
                if(QuranCanvas.line_on_page[line] == i)
                {
                    g.setFont(QuranCanvas.font);
                    g.drawString("Ayat : " + String.valueOf(line), 1, top, 0);
                    //load sound and play...

                    //playayats += String.valueOf(line)+ "|";

                    Quran.PlaySound(String.valueOf(line));
                    
                    System.out.println("Ayat "+String.valueOf(line));
                    top += 2;
                }
                g.setFont(QuranCanvas.font);
                //untuk meulis arab
                if(pilih == 0 || pilih ==1){
                if(j2 != 0 && QuranCanvas.lines_type[i + 1] == '\001')
                {
                    canvas.drawLineArabic(g, canvas.buffer_line_arabic, j2 - 1, top + QuranCanvas.font.getHeight() + 4);
                    top += 26;
                    j2 = 0;
                    if(QuranCanvas.lines_type.length >= i + 2 && QuranCanvas.lines_type[i + 2] == '\002')
                        top += 6;
                }
                }
                //Untuk menulis arti
                if(pilih == 0 || pilih ==2){
                if(QuranCanvas.lines_type[i + 1] == '\002')
                {
                    g.drawChars(ac, 0, i2 - 1, 4, top + 4, 0);
                    top += QuranCanvas.font_height;
                    if(QuranCanvas.lines_type.length >= i + 2 && QuranCanvas.lines_type[i + 2] == '\001')
                        top += 4;
                }
                }
                if(i >= QuranCanvas.page - 1 || flag7)
                    return;
            }
            catch(Exception exception2)
            {
                exception2.printStackTrace();
            }
        }

        Quran.PlaySound("play");
    }

    public String replaceAll(String text, String searchString, String replacementString)
    {
        StringBuffer sBuffer = new StringBuffer();
        for(int pos = 0; (pos = text.indexOf(searchString)) != -1;)
        {
            sBuffer.append(text.substring(0, pos) + replacementString);
            text = text.substring(pos + searchString.length());
        }

        sBuffer.append(text);
        return sBuffer.toString();
    }

    private QuranCanvas canvas;
    private static final int LEFT = 4;
}
